package com.smy.maven.plugin;

import com.itextpdf.text.*;
import com.itextpdf.text.Font;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.smy.tool.api.domain.SmyApiFieldInfo;
import com.smy.tool.api.domain.SmyApiMethodInfo;
import com.smy.tool.api.domain.SmyApiServiceInfo;


import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugin.logging.SystemStreamLog;

import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Description:
 *
 * @author 
 * @since 2017/12/2.
 */
public class ReportApiPdf {

    /**
     * PDF标题
     */
    private String titleName;

    /**
     * 输出路径
     */
    private String outputDir;

    /**
     * 接口信息列表
     */
    private List<SmyApiServiceInfo> serviceInfoList;

    /**
     * Maven Log
     */
    private Log log = new SystemStreamLog();

    /**
     * 构造函数
     *
     * @param titleName       PDF标题
     * @param outputDir       输出路径
     * @param serviceInfoList 接口信息列表
     */
    public ReportApiPdf(String titleName, String outputDir, List<SmyApiServiceInfo> serviceInfoList) {
        this.titleName = titleName;
        this.outputDir = outputDir;
        this.serviceInfoList = serviceInfoList;
    }

    /**
     * 生成PDF
     *
     * @throws IOException
     * @throws DocumentException
     */
    public void reportPdf() throws IOException, DocumentException {
        Document document = new Document();
        FileOutputStream out = new FileOutputStream(outputDir + File.separator + titleName + ".pdf");
        BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);
        Font fontZH = new Font(bfChinese, 8.0F, 0);
        Font fontTitle = new Font(bfChinese, 16.0F, 0);
        Font fontEG = FontFactory.getFont(BaseFont.COURIER, 8.0F, 0);
        BaseColor serviceBgColor = new BaseColor(Color.magenta);
        BaseColor methodBgColor = new BaseColor(Color.orange);

        PdfWriter.getInstance(document, out);
        document.open();

        //画标题
        Paragraph titleParagraph = new Paragraph(new Chunk(titleName, fontTitle).setLocalDestination(titleName));
        titleParagraph.setAlignment(Element.ALIGN_CENTER);
        document.add(titleParagraph);
        document.add(new Paragraph("\n"));

        //画表格
        for (SmyApiServiceInfo smyApiServiceInfo : serviceInfoList) {
            PdfPTable serviceTable = new PdfPTable(2);
            serviceTable.setWidthPercentage(100.0F);
            serviceTable.setWidths(new float[] { 30, 70 });

            PdfPCell serviceCell = new PdfPCell(new Paragraph("接口描述", fontZH));
            serviceCell.setColspan(2);
            serviceCell.setBackgroundColor(serviceBgColor);
            serviceTable.addCell(serviceCell);

            serviceTable.addCell(new Paragraph("接口名称", fontZH));
            PdfPCell serviceNameCell = new PdfPCell(new Paragraph(smyApiServiceInfo.getServiceName(), fontEG));
            serviceTable.addCell(serviceNameCell);

            serviceTable.addCell(new Paragraph("描述", fontZH));
            PdfPCell serviceDescCell = new PdfPCell(new Paragraph(smyApiServiceInfo.getServiceDesc(), fontZH));
            serviceTable.addCell(serviceDescCell);

            serviceTable.addCell(new Paragraph("负责人", fontZH));
            PdfPCell serviceAuthorCell = new PdfPCell(new Paragraph(smyApiServiceInfo.getServiceAuthor(), fontZH));
            serviceTable.addCell(serviceAuthorCell);
            document.add(serviceTable);
            document.add(new Paragraph("\n"));
            document.add(new Paragraph("\n"));

            for (SmyApiMethodInfo smyApiMethodInfo : smyApiServiceInfo.getMethodInfoList()) {
                PdfPTable methodTable = new PdfPTable(4);
                methodTable.setWidthPercentage(100.0F);
                methodTable.setWidths(new float[] { 40, 10, 40, 10 });

                PdfPCell methodCell = new PdfPCell(new Paragraph("方法描述", fontZH));
                methodCell.setColspan(4);
                methodCell.setBackgroundColor(methodBgColor);
                methodTable.addCell(methodCell);

                methodTable.addCell(new Paragraph("方法名称", fontZH));
                PdfPCell methodNameCell = new PdfPCell(new Paragraph(smyApiMethodInfo.getMethodName(), fontEG));
                methodNameCell.setColspan(3);
                methodTable.addCell(methodNameCell);

                methodTable.addCell(new Paragraph("描述", fontZH));
                PdfPCell methodDescCell = new PdfPCell(new Paragraph(smyApiMethodInfo.getMethodDesc(), fontZH));
                methodDescCell.setColspan(3);
                methodTable.addCell(methodDescCell);

                for (Map.Entry<String, List<SmyApiFieldInfo>> entry : smyApiMethodInfo.getInputParamMap().entries()) {

                    PdfPCell inputCellDesc = new PdfPCell(new Paragraph("请求参数", fontZH));
                    inputCellDesc.setBackgroundColor(methodBgColor);
                    methodTable.addCell(inputCellDesc);

                    PdfPCell inputValueCell = new PdfPCell(new Paragraph(entry.getKey(), fontEG));
                    inputValueCell.setColspan(3);
                    inputValueCell.setBackgroundColor(methodBgColor);
                    methodTable.addCell(inputValueCell);

                    methodTable.addCell(new Paragraph("参数名", fontZH));
                    methodTable.addCell(new Paragraph("参数类型", fontZH));
                    methodTable.addCell(new Paragraph("描述", fontZH));
                    methodTable.addCell(new Paragraph("可否为空", fontZH));
                    for (SmyApiFieldInfo smyApiFieldInfo : entry.getValue()) {
                        methodTable.addCell(new Paragraph(smyApiFieldInfo.getFieldName(), fontEG));
                        methodTable.addCell(new Paragraph(smyApiFieldInfo.getFieldType(), fontEG));
                        methodTable.addCell(new Paragraph(smyApiFieldInfo.getFieldDesc(), fontZH));
                        methodTable.addCell(new Paragraph(smyApiFieldInfo.isNotNull() ? "false" : "true", fontEG));
                    }
                }

                for (Map.Entry<String, List<SmyApiFieldInfo>> entry : smyApiMethodInfo.getOutputParamMap().entrySet()) {
                    PdfPCell outputCellDesc = new PdfPCell(new Paragraph("响应参数", fontZH));
                    outputCellDesc.setBackgroundColor(methodBgColor);
                    methodTable.addCell(outputCellDesc);

                    PdfPCell outputValueCell = new PdfPCell(new Paragraph(entry.getKey(), fontEG));
                    outputValueCell.setColspan(3);
                    outputValueCell.setBackgroundColor(methodBgColor);
                    methodTable.addCell(outputValueCell);

                    for (SmyApiFieldInfo smyApiFieldInfo : entry.getValue()) {
                        methodTable.addCell(new Paragraph(smyApiFieldInfo.getFieldName(), fontEG));
                        methodTable.addCell(new Paragraph(smyApiFieldInfo.getFieldType(), fontEG));
                        methodTable.addCell(new Paragraph(smyApiFieldInfo.getFieldDesc(), fontZH));
                        methodTable.addCell(new Paragraph(smyApiFieldInfo.isNotNull() ? "false" : "true", fontEG));
                        if (null != smyApiFieldInfo.getSubFieldInfoList()) {
                            for (SmyApiFieldInfo subFieldInfo : smyApiFieldInfo.getSubFieldInfoList()) {
                                methodTable.addCell(new Paragraph(
                                        "|----" + smyApiFieldInfo.getFieldName() + "." + subFieldInfo.getFieldName(),
                                        fontEG));
                                methodTable.addCell(new Paragraph(subFieldInfo.getFieldType(), fontEG));
                                methodTable.addCell(new Paragraph(subFieldInfo.getFieldDesc(), fontZH));
                                methodTable.addCell(new Paragraph(subFieldInfo.isNotNull() ? "false" : "true", fontEG));
                                if (null != subFieldInfo.getSubFieldInfoList()) {
                                    for (SmyApiFieldInfo grandsonFieldInfo : subFieldInfo.getSubFieldInfoList()) {
                                        methodTable.addCell(new Paragraph(
                                                "|----" + smyApiFieldInfo.getFieldName() + "." +
                                                subFieldInfo.getFieldName() + "." + grandsonFieldInfo.getFieldName(),
                                                fontEG));
                                        methodTable.addCell(new Paragraph(grandsonFieldInfo.getFieldType(), fontEG));
                                        methodTable.addCell(new Paragraph(grandsonFieldInfo.getFieldDesc(), fontZH));
                                        methodTable.addCell(
                                                new Paragraph(grandsonFieldInfo.isNotNull() ? "false" : "true",
                                                        fontEG));
                                    }
                                }
                            }
                        }
                    }
                }
                document.add(methodTable);
                document.add(new Paragraph("\n"));
                document.add(new Paragraph("\n"));
            }
            document.add(new Paragraph("\n"));
            document.add(new Paragraph("\n"));
        }

        document.add(new Paragraph("\n"));
        document.close();

        log.info("成功导出pdf到：" + outputDir);
    }
}
